package cn.jcet.mapreduce.kv;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.KeyValueLineRecordReader;
import org.apache.hadoop.mapreduce.lib.input.KeyValueTextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

/**
 * @author 鲜磊
 * @Date 2020/12/7 22:39
 */
public class KVTextDriver {

    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        args = new String[]{"e:/code/kvinput","e:/code/kvoutput1"};
        Configuration conf = new Configuration();
        conf.set(KeyValueLineRecordReader.KEY_VALUE_SEPERATOR," ");
        // 1、获取Job对象
        Job job = Job.getInstance(conf);
        //2、设置jar存放位置
        job.setJarByClass(KVTextDriver.class);
        //3、关联mapper和reduce类
        job.setMapperClass(KVTextMapper.class);
        job.setReducerClass(KVTextReduce.class);
        //4、设置map的key和value输出数据类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);
        //5、设置最总的输出类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        // 设置输入格式
        job.setInputFormatClass(KeyValueTextInputFormat.class);
        //6、设置输出输出路径
        FileInputFormat.setInputPaths(job,new Path(args[0]));
        FileOutputFormat.setOutputPath(job,new Path(args[1]));
        //7、提交任务
        boolean result = job.waitForCompletion(true);
        System.exit(result?0:1);
    }
}
